#pragma once
#include "misc.h"


MaterialStore* MaterialStore::_instance=NULL;
MaterialStore::MaterialStore()
{
	//MessageBox(0,L"MaterialStore::ctor",0,0);

	_bkgroundBrush = CreateSolidBrush(0xEEEEEE);
	_foregroundColor = RGB(0, 128, 192);

	_western = CreateCompatibleFont(1033);
	_eastern = CreateCompatibleFont(2052);
}



MaterialStore::~MaterialStore()
{
	DeleteObject(_bkgroundBrush);
	DeleteObject(_western);
	DeleteObject(_eastern);

	//MessageBox(0,L"MaterialStore::dector",0,0);
}

HFONT MaterialStore::CreateCompatibleFont(LCID locale)
{
	std::vector<std::wstring> fontList;
	switch(locale)
	{
	case 2052:
		fontList.push_back(L"Microsoft YaHei");
		fontList.push_back(L"SimSun");
		break;
	case 1033:
	default:
		fontList.push_back(L"Segoe UI");
		fontList.push_back(L"Calibri");
		fontList.push_back(L"Lucida Sans");
		break;
	}


	for(int i=0; i<(int)fontList.size(); i++)
	{
		HFONT hFont = CreateFont(18,0,0,0,FW_DONTCARE,FALSE,FALSE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS,
				CLIP_DEFAULT_PRECIS,CLEARTYPE_QUALITY, VARIABLE_PITCH, fontList[i].c_str());
		if(hFont)
		{
			return hFont;
		}	
	}

	return NULL;
}

void MaterialStore::Destory()
{
	if(_instance)
	{
		delete _instance;
	}
}
const MaterialStore* MaterialStore::Current()
{
	if(!_instance)
	{
		_instance = new MaterialStore();
	}
	return _instance;
}

const HBRUSH MaterialStore::BackgroundBrush() const
{
	return _bkgroundBrush;
}
const COLORREF MaterialStore::ForegroundColor() const
{
	return _foregroundColor;
}
const HFONT MaterialStore::WesternFont() const
{
	return _western;
}
const HFONT MaterialStore::EasternFont() const
{
	return _eastern;
}


void SetWindowFont(ATL::CWindow& ctl, LCID locale)
{
	if(!locale)
		locale = GetUserDefaultLCID();
	
	HFONT hFont;
	switch(locale)
	{
	case 2052:
		hFont = MaterialStore::Current()->EasternFont();
		break;
	case 1033:
	default:
		hFont = MaterialStore::Current()->WesternFont();
		break;
	}

	if(hFont)
		ctl.SetFont(hFont);
}